package com.markyao.controller;

import com.markyao.dao.pojo.Address;
import com.markyao.dao.pojo.User;
import com.markyao.service.AddressService;
import com.markyao.service.DictDistrictService;
import com.markyao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("address")
public class AddressController {

    @Autowired
    AddressService addressService;
    @Autowired
    DictDistrictService dictDistrictService;

    @PostMapping("add")
    private Result addAddress(Address address, HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();
        address.setUid(uid);
        return addressService.addAddress(address);
    }

    @GetMapping
    private Result getProvince(String parent){
        return dictDistrictService.findByParent(parent);
    }

    @GetMapping("list")
    public Result getAddress(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();
        return addressService.findByUid(uid);
    }

    @PostMapping("setdefault")
    public Result setdefault(Integer aid,HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();
        return addressService.setDefault(aid,uid);
    }
    @PostMapping("delete")
    public Result delete(Integer aid,HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer uid = user.getUid();
        return addressService.delete(aid,uid);
    }
}
